package app;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class SocketConnection {

	public SocketConnection(Socket s) {
		try {
			socket = s;
			out = new ObjectOutputStream(socket.getOutputStream());
		} 
		catch (IOException e) {e.printStackTrace();}
	}
	
	public void send(Object o) throws IOException {
		out.writeObject(o);
		out.flush();
	}
	
	public Object receive() throws IOException, ClassNotFoundException {
		if (in == null)
			in = new ObjectInputStream(socket.getInputStream());
		return in.readObject();
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public void close() {
		try {
			if (in != null)  in.close();
			if (out != null) out.close();
			if (socket != null) socket.close();
		}
		catch (IOException e) {e.printStackTrace();}
	}
	
	private User user;
	protected Socket socket;
	protected ObjectInputStream in;
	protected ObjectOutputStream out;
}
